package com.engine4cloud.tdf.supervision.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.core.constant.enums.CorAuditActionStatusEnum;
import com.engine4cloud.tdf.common.core.constant.enums.CorFileTypeEnum;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.common.data.web.query.Queries;
import com.engine4cloud.tdf.common.security.service.TdfUser;
import com.engine4cloud.tdf.common.security.util.SecurityUtils;
import com.engine4cloud.tdf.supervision.convert.JzdxReleaseConvert;
import com.engine4cloud.tdf.supervision.dto.create.JzdxReleaseCreateDTO;
import com.engine4cloud.tdf.supervision.dto.query.JzdxFileQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxReleaseResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.JzdxReleaseUpdateDTO;
import com.engine4cloud.tdf.supervision.dto.update.JzdxUpdateStatusDTO;
import com.engine4cloud.tdf.supervision.entity.JzdxReleaseEntity;
import com.engine4cloud.tdf.supervision.mapper.JzdxReleaseMapper;
import com.engine4cloud.tdf.supervision.service.*;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;
import java.util.Optional;

/**
 * 矫正对象-解除
 *
 * @author wenjie
 * @date 2024-04-11 17:40:57
 */
@Service
public class JzdxReleaseServiceImpl extends ServiceImpl<JzdxReleaseMapper, JzdxReleaseEntity> implements JzdxReleaseService {
    @Resource
    private JzdxService jzdxService;

    @Resource
    private JzdxAuditResultService auditResultService;

    @Resource
    private JzdxTerminalManageService terminalManageService;

    @Resource
    private JzdxFileService fileService;

    @Override
    public JzdxReleaseResultDTO findByCode(String code) {
        JzdxReleaseResultDTO result = JzdxReleaseConvert.INSTANCE.convert(this.getOne(wrapper(code)));
        if (Objects.isNull(result)) {
            return null;
        }
        //设置法律文书
        this.setInstrumentList(result);
        return result;
    }


    @Override
    public JzdxReleaseResultDTO getById(Long id) {
        JzdxReleaseResultDTO result = Optional.ofNullable(JzdxReleaseConvert.INSTANCE.convert(super.getById(id)))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        //设置法律文书
        this.setInstrumentList(result);
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean create(JzdxReleaseCreateDTO dto) {
        JzdxResultDTO dx = Optional.ofNullable(jzdxService.getByCode(dto.getRybh()))
                .orElseThrow(() -> new ValidateCodeException("矫正对象数据不存在"));

        //保存主数据
        TdfUser user = SecurityUtils.getUser();
        JzdxReleaseEntity entity = JzdxReleaseConvert.INSTANCE.create(dto, user.getTenantId());
        super.save(entity);
        //保存文件信息
        this.saveInstrumentList(dto, entity, user);
        //设置矫正对象的状态
        auditResultService.setStatus(new JzdxUpdateStatusDTO(dto.getRybh(), dx.getDeptId(), CorAuditActionStatusEnum.RELEASE));
        //解绑设备
        terminalManageService.removeByCode(dx.getRybh());
        return true;
    }

    @Override
    public Boolean update(JzdxReleaseUpdateDTO dto) {
        JzdxReleaseEntity old = Optional.ofNullable(super.getById(dto.getId()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        TdfUser user = SecurityUtils.getUser();
        super.updateById(JzdxReleaseConvert.INSTANCE.update(dto));
        //设置附件信息
        this.updateInstrumentList(dto, old, user);
        return true;
    }

    public void saveInstrumentList(JzdxReleaseCreateDTO dto, JzdxReleaseEntity entity, TdfUser user) {
        if (CollectionUtils.isEmpty(dto.getInstrumentList())) {
            return;
        }

        fileService.batchSaveFile(new JzdxFileQueryDTO(entity.getRybh(), CorFileTypeEnum.CORRECTOR_RELEASE), dto.getInstrumentList(), user);
    }

    public void updateInstrumentList(JzdxReleaseUpdateDTO dto, JzdxReleaseEntity entity, TdfUser user) {
        fileService.batchUpdateFile(new JzdxFileQueryDTO(entity.getRybh(), CorFileTypeEnum.CORRECTOR_RELEASE), dto.getInstrumentList(), user);
    }

    private void setInstrumentList(JzdxReleaseResultDTO dto) {
        JzdxFileQueryDTO query = new JzdxFileQueryDTO(dto.getRybh(), CorFileTypeEnum.CORRECTOR_RELEASE);
        dto.setInstrumentList(fileService.findList(query));
    }

    private LambdaQueryWrapper<JzdxReleaseEntity> wrapper(String code) {
        LambdaQueryWrapper<JzdxReleaseEntity> wrapper = Queries.lambda();
        Queries.accept(code, v -> wrapper.eq(JzdxReleaseEntity::getRybh, v));
        return wrapper;
    }

}